package com.example.custsecurity.security.config;

import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
//实现一个自定义的密码加密器
public class CustPasswordEncoder implements PasswordEncoder {
    /*
    * encode 对前端输入的密码加密
    * */
    @Override
    public String encode(CharSequence charSequence) {
        log.debug("......前端输入的密码"+charSequence);
        log.debug("......加密后的密码"+MD5.create().digestHex16("123"));
        return MD5.create().digestHex16(charSequence.toString());
    }

    /*
    * matches：密码的比对
    * charSequence：页面输入的密码
    * password：数据库中保存的已加密的密码
    * */
    @Override
    public boolean matches(CharSequence charSequence, String password) {
        String md5_password = MD5.create().digestHex16(charSequence.toString());
        log.debug("......数据库密码"+password);
        return md5_password.equals(password);
    }
}
